package com.tmeone.erpone.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmeone.erpone.entity.Customer;
import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tmeone
 * @since 2021-04-19
 */
@RestController
@RequestMapping
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    private String customerId;

    private String tparId;

    @GetMapping("/findCustomer")
    public JsonResult customer(@RequestParam(value = "key",required = false)String key){
        List<Customer> customers = customerService.findCustomerByKey(key);
        return new JsonResult(1,customers);
    }

    @GetMapping("/customer")
    public JsonResult customers(@RequestParam(value = "current",required = false,defaultValue = "1")Integer current,@RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize){
        Page<Customer> customerPage = new Page<>(current, pageSize);
        IPage<Customer> pageCustomer = customerService.getPageCustomer(customerPage);
        return new JsonResult(1,pageCustomer);
    }
    @PostMapping("/customer/customerAdd")
    public JsonResult addCustomer(@RequestBody Customer customer){
        customerService.save(customer);
        return new JsonResult(1,"ok");
    }
    @PutMapping("/customer/customerUpdate")
    public JsonResult updateCustomer(@RequestBody Customer customer){
        if(customerService.updateById(customer)){
            return new JsonResult(1,"ok");
        }
        return new JsonResult(0,null);
    }

    @DeleteMapping("/deleteCustomer/{id}")
    public JsonResult deleteCustomer(@PathVariable String id){
        if(customerService.removeById(id)){
            return new JsonResult(1,"ok");
        }
        return new JsonResult(0,null);
    }
    @RequestMapping( value = "/contractApply/{id}",method = RequestMethod.GET)
    public void contractApplyCustomer(@PathVariable String id){
       this.customerId=id;
        System.out.println(customerId);
    }
    @RequestMapping( value = "/contractAudit/{ComB}/{id}",method = RequestMethod.GET)
    public void contractAuditCustomer(@PathVariable String ComB,@PathVariable String id){
        this.customerId=ComB;
        this.tparId = id;
        System.out.println(customerId);
    }
    @RequestMapping(value = "/customer1",method = RequestMethod.GET)
    public JsonResult customer1Customer(){
        String customerId = this.customerId;
        Customer customer = customerService.getById(customerId);
        customer.setParId(this.tparId);
        System.out.println(customer);
        return new JsonResult(1,customer);
    }
}
